package org.huzhp.gateway.filter;

import org.huzhp.gateway.filter.balance.GatewayLoadBalanceClient;
import org.huzhp.gateway.filter.balance.LoadBalancerGatewayFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Description: 负载均衡过滤器
 * @Author: huzp
 * @CreateDate: 2020/4/13 14:37
 * @Version: 1.0
 */
@Component
public class LoadBalancerGatewayFilterFactory extends CommonGatewayFilterFactory<LoadBalancerGatewayFilterFactory.Config> {

    @Autowired
    private GatewayLoadBalanceClient gatewayLoadBalanceClient;

    @Override
    public List<String> shortcutFieldOrder() {
        return Arrays.asList("services");
    }

    public LoadBalancerGatewayFilterFactory() {
        super(Config.class);
    }

    @Override
    public GatewayFilter apply(Config config) {
        return new LoadBalancerGatewayFilter(gatewayLoadBalanceClient,config.getRule(),config.getServers());
    }

    public static class Config {

        private String rule;

        private List<String> servers;

        public String getRule() {
            return rule;
        }

        public void setRule(String rule) {
            this.rule = rule;
        }

        public List<String> getServers() {
            return servers;
        }

        public void setServers(List<String> servers) {
            this.servers = servers;
        }
    }
}
